﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MonitorPlan.Admin
{
    public partial class Room : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["userid"] == null)
                {
                    Response.Redirect("/Admin");
                }
            }
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            this.rp1.DataBind();
            this.mv.ActiveViewIndex = 0;
        }

        protected void btnView_Click1(object sender, EventArgs e)
        {
            this.rp1.DataBind();
            this.mv.ActiveViewIndex = 0;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.mv.ActiveViewIndex = 1;
            this.vAddRoom_txtRoomName.Text = "";
            this.vAddRoom_txtRoomDescription.Text = "";
            this.actionSms.InnerHtml = "";
            this.vAddRoom_txtRoomName.Focus();
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            this.rp1.DataBind();
            this.mv.ActiveViewIndex = 0;
        }

        // cancel adding new room
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.vAddRoom_txtRoomName.Text = "";
            this.vAddRoom_txtRoomDescription.Text = "";
            this.actionSms.InnerHtml = "";
            this.vAddRoom_txtRoomName.Focus();
        }

        // save a new room
        protected void btnSave_Click(object sender, EventArgs e)
        {
            String sms;
            Model.Room r = new Model.Room();
            r.Name = this.vAddRoom_txtRoomName.Text;
            r.Description = this.vAddRoom_txtRoomDescription.Text;
            r.HouseId = Convert.ToInt32(this.vAddRoom_txtHouseName.SelectedValue);
            if(this.vAddRoom_txtRoomName.Text!="")
            {
                if(r.Add())
                {
                    sms = "<div class='alert alert-success'>";
                    sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                    sms += "Data has been save successfully!";
                    sms += "</div>";
                    this.vAddRoom_txtRoomName.Text = "";
                    this.vAddRoom_txtRoomDescription.Text = "";
                    this.vAddRoom_txtRoomName.Focus();
                    this.actionSms.InnerHtml = sms;
                }
                else
                {
                    sms = "<div class='alert alert-error'>";
                    sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                    sms += "Data has not been saved successfully. Try again!";
                    sms += "</div>";
                    this.actionSms.InnerHtml = sms;
                }
            }
            else
            {
                this.actionSms.InnerHtml = "<span style='color:red'>Please input room name!</span>";
            }
        }


        protected void rp1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            // delete a room when you user click on delete button in repeater.
            if(e.CommandName=="delete")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                Model.Room r = new Model.Room();
                if(r.Remove(id))
                {
                    this.rp1.DataBind();
                    this.mv.ActiveViewIndex = 0;
                }
            }
            // view room in detail
            if(e.CommandName=="detail")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                Model.Room r = new Model.Room().getRoom(id);
                this.lbRoomId.Text = r.Id.ToString();
                this.lbRoomName.Text = r.Name.ToString();
                this.lbRoomHouseName.Text = r.HouseName.ToString();
                this.lbRoomDescription.Text = r.Description.ToString();
                this.lbRoomIsDeleted.Text = r.IsDeleted.ToString();
                this.mv.ActiveViewIndex = 2;
                
            }
            // edit each room
            if (e.CommandName == "edit")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                this.mv.ActiveViewIndex = 3;
                Model.Room r = new Model.Room().getRoom(id);
                this.txtRname.Text = r.Name;
                this.txtHName.SelectedIndex = this.txtHName.Items.IndexOf(this.txtHName.Items.FindByText(r.HouseName));
                this.txtRDescription.Text = r.Description;
                this.txtid.Value = id.ToString();
            }
        }

        // link to add house form when user click on new house in room form.
        protected void btnAddHouse_Click(object sender, EventArgs e)
        {
            Session["new_house"] = "new";
            Response.Redirect("/Admin/House.aspx");
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            this.mv.ActiveViewIndex = 0;
        }

        protected void btnNewHouse_Click(object sender, EventArgs e)
        {
            Session["new_house"] = "new";
            Response.Redirect("/Admin/House.aspx");
        }

        protected void btnCancelUpdate_Click(object sender, EventArgs e)
        {
            this.mv.ActiveViewIndex = 0;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if(this.txtRname.Text!="")
            {
                Model.Room r = new Model.Room();
                r.Name = this.txtRname.Text;
                r.HouseId = Convert.ToInt32(this.txtHName.SelectedValue);
                r.Description = this.txtRDescription.Text;
                int id = Convert.ToInt32(this.txtid.Value);
                if (r.Update(id))
                {
                    this.rp1.DataBind();
                    this.mv.ActiveViewIndex = 0;
                }
                else
                    this.updateSms.InnerHtml = "Cannot update data. Try again!";
            }
        }
    }
}